package org.xbzheng.jpa.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Created by xbzheng on 2018/7/16.
 *
 * @Bean声明的方法名称是最后bean的名称，所以要特别注意
 *
 * 如 下面的EntityManageFactory生成方法的名称是entityManagerFactoryBean。 所以在spring容器中EntityManageFactory的名称是entityManagerFactoryBean
 * 而不是entityManagerFactory，因为@EnableJpaRepositories注解属性entityManagerFactoryRef默认指定的是entityManagerFactory，所以我们需要手动申明
 *
 */
@Configuration
@PropertySource("classpath:jdbc.properties")
@EnableJpaRepositories(basePackages = "org.xbzheng.jpa.dao", entityManagerFactoryRef = "entityManagerFactoryBean")
public class JdbcConfig {

    @Bean(destroyMethod = "close")
    public DataSource dataSource(Environment env){
        HikariConfig config = new HikariConfig();
        config.setDriverClassName(env.getRequiredProperty("db.driver"));
        config.setJdbcUrl(env.getRequiredProperty("db.url"));
        config.setUsername(env.getRequiredProperty("db.username"));
        config.setPassword(env.getRequiredProperty("db.password"));

        return new HikariDataSource(config);
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter(){
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.H2);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(false);
        return adapter;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(JpaVendorAdapter jpaVendorAdapter, DataSource dataSource, Environment env){
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        bean.setDataSource(dataSource);
        bean.setJpaVendorAdapter(jpaVendorAdapter);
//        bean.setPersistenceProvider(new HibernatePersistenceProvider());
        //实体所在位置
        bean.setPackagesToScan("org.xbzheng.jpa.bean");

        Properties jpaProperties = new Properties();

        jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));

        jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));

        jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy"));

        jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));

        jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));

        bean.setJpaProperties(jpaProperties);

        return bean;
    }

}
